package com.ja.openchat.views;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.ja.openchat.R;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;

import androidx.annotation.NonNull;

/**
 * 修改昵称
 */
public class SetPwdDialog extends BottomPopupView {

    private final OnButtonClickListener listener;
    private Context context;
    private EditText et_pwd;
    private EditText et_pwd1;
    private EditText et_pwd2;
    private ImageView iv_swich_passwrod;
    private ImageView iv_swich_passwrod1;
    private ImageView iv_swich_passwrod2;
    private ImageView iv_cancel;
    private TextView tv_confirm;
    private TextView tv_title;
    private ImageView edit_clear_1;
    private ImageView edit_clear_2;

    private boolean hasPassword;

    public SetPwdDialog(@NonNull Context context, boolean hasPassword, OnButtonClickListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
        this.hasPassword = hasPassword;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_set_pwd;
    }


    @Override
    protected void onCreate() {
        super.onCreate();

        et_pwd = findViewById(R.id.et_pwd);
        et_pwd1 = findViewById(R.id.et_pwd_1);
        et_pwd2 = findViewById(R.id.et_pwd_2);
        iv_swich_passwrod = findViewById(R.id.iv_swich_passwrod);
        iv_swich_passwrod1 = findViewById(R.id.iv_swich_passwrod_1);
        iv_swich_passwrod2 = findViewById(R.id.iv_swich_passwrod_2);
        iv_cancel = findViewById(R.id.iv_cancel);
        tv_confirm = findViewById(R.id.tv_confirm);
        tv_title = findViewById(R.id.tv_title);

        edit_clear_1 = findViewById(R.id.edit_clear_1);
        edit_clear_2 = findViewById(R.id.edit_clear_2);

        LinearLayout llPasswordOld = findViewById(R.id.llPasswordOld);
        View lnePasswordOld = findViewById(R.id.linePasswordOld);
        if(!hasPassword){
            llPasswordOld.setVisibility(GONE);
            lnePasswordOld.setVisibility(GONE);
            tv_title.setText(context.getString(R.string.set_login_password_text));
        }else {
            tv_title.setText(context.getString(R.string.modify_login_password_text));
        }

        iv_swich_passwrod.setImageResource(R.drawable.setting_pwd_invisble);
        et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        iv_swich_passwrod.setTag(false);
        iv_swich_passwrod.setOnClickListener(v -> {
            boolean isShow = (boolean) iv_swich_passwrod.getTag();
            if(!isShow){
                //显示
                iv_swich_passwrod.setImageResource(R.drawable.setting_pwd_visible);
                et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                iv_swich_passwrod.setTag(true);
            }else {
                //隐藏
                iv_swich_passwrod.setImageResource(R.drawable.setting_pwd_invisble);
                et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                iv_swich_passwrod.setTag(false);
            }
        });

//        iv_swich_passwrod1.setImageResource(R.drawable.setting_pwd_invisble);
//        et_pwd1.setTransformationMethod(PasswordTransformationMethod.getInstance());
//        iv_swich_passwrod1.setTag(false);
//        iv_swich_passwrod1.setOnClickListener(v -> {
//            boolean isShow = (boolean) iv_swich_passwrod1.getTag();
//            if(!isShow){
//                //显示
//                iv_swich_passwrod1.setImageResource(R.drawable.setting_pwd_visible);
//                et_pwd1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//                iv_swich_passwrod1.setTag(true);
//            }else {
//                //隐藏
//                iv_swich_passwrod1.setImageResource(R.drawable.setting_pwd_invisble);
//                et_pwd1.setTransformationMethod(PasswordTransformationMethod.getInstance());
//                iv_swich_passwrod1.setTag(false);
//            }
//        });

//        iv_swich_passwrod2.setImageResource(R.drawable.setting_pwd_invisble);
//        et_pwd2.setTransformationMethod(PasswordTransformationMethod.getInstance());
//        iv_swich_passwrod2.setTag(false);
//        iv_swich_passwrod2.setOnClickListener(v -> {
//            boolean isShow = (boolean) iv_swich_passwrod2.getTag();
//            if(!isShow){
//                //显示
//                iv_swich_passwrod2.setImageResource(R.drawable.setting_pwd_visible);
//                et_pwd2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//                iv_swich_passwrod2.setTag(true);
//            }else {
//                //隐藏
//                iv_swich_passwrod2.setImageResource(R.drawable.setting_pwd_invisble);
//                et_pwd2.setTransformationMethod(PasswordTransformationMethod.getInstance());
//                iv_swich_passwrod2.setTag(false);
//            }
//        });

        et_pwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setButton();
            }
        });

        et_pwd1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setButton();
                String et1 = et_pwd1.getText().toString().trim();
                if(!TextUtils.isEmpty(et1)){
                    edit_clear_1.setVisibility(VISIBLE);
                }else {
                    edit_clear_1.setVisibility(INVISIBLE);
                }
            }
        });

        et_pwd2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setButton();
                String et1 = et_pwd2.getText().toString().trim();
                if(!TextUtils.isEmpty(et1)){
                    edit_clear_2.setVisibility(VISIBLE);
                }else {
                    edit_clear_2.setVisibility(INVISIBLE);
                }
            }
        });

        edit_clear_1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                et_pwd1.setText("");
            }
        });

        edit_clear_2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                et_pwd2.setText("");
            }
        });

        iv_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        tv_confirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String et = et_pwd.getText().toString().trim();
                String et1 = et_pwd1.getText().toString().trim();
                String et2 = et_pwd2.getText().toString().trim();
                if (hasPassword) {
                    if (TextUtils.isEmpty(et)) {
                        ToastUtils.showShort(context.getString(R.string.enter_old_pwd));
                        return;
                    }
                }
                if (TextUtils.isEmpty(et1)) {
                    ToastUtils.showShort(context.getString(R.string.edit_new_password_hint));
                    return;
                }
                if(et1.length()<6 || et1.length()>20){
                    ToastUtils.showShort(context.getString(R.string.edit_account_password_hint));
                    return;
                }
                if (TextUtils.isEmpty(et2)) {
                    ToastUtils.showShort(context.getString(R.string.repeat_new_password_hint));

                    return;
                }
                if (!et1.equals(et2)) {
                    ToastUtils.showShort(context.getString(R.string.pwd_not_match));
                    return;
                }
                if(listener != null){
                    listener.onClick(et,et1,SetPwdDialog.this);
                }
            }
        });

    }

    private void setButton(){
        String et = et_pwd.getText().toString().trim();
        String et1 = et_pwd1.getText().toString().trim();
        String et2 = et_pwd2.getText().toString().trim();

        if(!TextUtils.isEmpty(et2)){
            edit_clear_2.setVisibility(VISIBLE);
        }else {
            edit_clear_2.setVisibility(INVISIBLE);
        }
        if(!TextUtils.isEmpty(et) && !TextUtils.isEmpty(et1) && !TextUtils.isEmpty(et2)){
            tv_confirm.setClickable(true);
            tv_confirm.setBackgroundResource(R.drawable.login_btn_bg_green_90);
        }else {
            tv_confirm.setClickable(false);
            tv_confirm.setBackgroundResource(R.drawable.login_btn_bg_green_90_unselect);
        }
    }

    public interface OnButtonClickListener {
        void onClick(String oldPassword, String newPassword,SetPwdDialog pwdDialog);
    }

//    @Override
//    protected int getPopupWidth() {
//        return (int) (XPopupUtils.getAppWidth(getContext())*0.9);
//    }

    @Override
    protected int getPopupHeight() {
        return (int) (XPopupUtils.getAppHeight(getContext())*0.98);
    }
}
